import { request, useUserStore, clearEmpty } from "jmash-core";
import type { AxiosPromise } from "axios";
import type { SmsTemplateApi, SmsTemplateReq,SmsTemplateModel,SmsTemplatePage,SmsTemplateList,
         SmsTemplateKey,SmsTemplateKeyList,SmsTemplateMoveKey,
         SmsCaptchaReq,
         SmsReap} from "./types";
import type { SmsAppConfigKey } from '../sms-app-config/types';
         
class SmsTemplateApiImpl implements SmsTemplateApi {
        
getKey(model: SmsTemplateModel): SmsTemplateKey {
  return { 
    templateId: model.templateId,
   } as SmsTemplateKey;
}

// 查询翻页信息短消息模板
findPage(query?: SmsTemplateReq): AxiosPromise<SmsTemplatePage> {
  const tenant=useUserStore().user.tenant;
  if(query){
    query.hasTemplateType=query.templateType !== undefined;
  }
  clearEmpty(query);
  return request({
    url: "/v1/sms/sms_template/page/"+tenant,
    method: "get",
    params: query,
  });
}

// 查询列表信息短消息模板
findList(query?: SmsTemplateReq): AxiosPromise<SmsTemplateList> {
  const tenant=useUserStore().user.tenant;
  clearEmpty(query);
  return request({
    url: "/v1/sms/sms_template/list/"+tenant,
    method: "get",
    params: query,
  });
}

// 查询短消息模板
findById(query: SmsTemplateKey): AxiosPromise<SmsTemplateModel> {
  const tenant=useUserStore().user.tenant;
  clearEmpty(query);
  return request({
    url: "/v1/sms/sms_template/id/"+tenant+"/"+query.templateId,
    method: "get",
  });
}


// 上移下移
move(key: SmsTemplateKey, up: boolean): AxiosPromise<boolean> {
  const data: SmsTemplateMoveKey = { ...key };
  data.tenant = useUserStore().user.tenant;
  data.up = up;
  clearEmpty(data);
  return request({
    url: "/v1/sms/sms_template/move",
    method: "put",
    data: data,
  });
}



// 删除短消息模板
delete(query: SmsTemplateKey): AxiosPromise<SmsTemplateModel> {
  query.tenant=useUserStore().user.tenant;
  clearEmpty(query);
  return request({
    url: "/v1/sms/sms_template/id",
    method: "delete",
    params: query,
  });
}

// 批量删除短消息模板
batchDelete(keys:SmsTemplateKey[]): AxiosPromise<number>{
  const ids = keys.map((item) => item.templateId);
  const data = {
    tenant: useUserStore().user.tenant,
    templateId: ids,
  } as SmsTemplateKeyList;
  clearEmpty(data);
  return request({
    url: "/v1/sms/sms_template/batch",
    method: "put",
    data: data,
  });
}

 // 同步短消息模版
 sync(query?: SmsAppConfigKey): AxiosPromise<boolean> {
  const tenant = useUserStore().user.tenant;
   clearEmpty(query);
   return request({
     url: "/v1/sms/sms_template/sync/" + tenant,
     method: "get",
     params: query,
   });
 }

// 发送短信
basic(data: SmsCaptchaReq): AxiosPromise<SmsReap> {
  data.tenant = useUserStore().user.tenant;
  if(data){
    data.hasProvider=data.provider !== undefined;
  }
  return request({
    url: "/v1/sms/basic",
    method: "post",
    data: data,
  });
}
} 
const smsTemplateApi = new SmsTemplateApiImpl();

export { smsTemplateApi };



